package com.xls.future.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.xls.future.R;
import com.xls.future.adapter.MyFragmentPagerAdapter;
import com.xls.future.fragment.FindFragment;
import com.xls.future.fragment.HomeFragment;
import com.xls.future.fragment.MineFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    @BindView(R.id.container)
    FrameLayout container;
    @BindView(R.id.navigation)
    BottomNavigationView navigation;


    private MyNavigationItemSelectedListener mOnNavigationItemSelectedListener;
    private MyFragmentPagerAdapter mPagerAdapter;
    private List<Fragment> fragments;
    private Fragment homeFragment;
    private Fragment findFragment;
    private Fragment mineFragment;
    private int curPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initViews();
    }


    private void initViews() {
        initFragments();
        mOnNavigationItemSelectedListener = new MyNavigationItemSelectedListener();
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    private void initFragments() {
        fragments = new ArrayList<>();
        homeFragment = new HomeFragment();
        findFragment = new FindFragment();
        mineFragment = new MineFragment();
        fragments.add(0, homeFragment);
        fragments.add(1, findFragment);
        fragments.add(2, mineFragment);
        switchFragment(0);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    class MyNavigationItemSelectedListener implements BottomNavigationView.OnNavigationItemSelectedListener {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    curPosition = 0;
                    item.expandActionView();
                    break;
                case R.id.navigation_find:
                    curPosition = 1;
                    break;
                case R.id.navigation_mine:
                    curPosition = 2;
                    break;

            }
            switchFragment(curPosition);
            return true;
        }
    }


    private void switchFragment(int position){
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        for(int i=0;i<fragments.size();i++){
            Fragment fragment = fragments.get(i);
            if(i==position){
                if(!fragment.isAdded()){
                    ft.add(R.id.container,fragment);
                }
                ft.show(fragment);
            }else{
                if(fragment.isAdded()){
                    ft.hide(fragment);
                }
            }
        }

        ft.commitAllowingStateLoss();
    }


}
